﻿@model CustomerModel.ChangePasswordModel

@{
    async Task RenderForm()
    {
        @if (ViewContext.ViewData.ModelState.ContainsKey(string.Empty))
        {
            <div asp-validation-summary="ModelOnly"></div>
        }
        <form>
            <div class="adminContent">
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="Password" />
                    </div>
                    <div class="adminData">
                        <input asp-for="Password" type="password" />
                        <span asp-validation-for="Password"></span>
                    </div>
                </div>
                <div class="adminRow">
                    <div class="adminTitle">
                        <smart-label asp-for="ConfirmPassword" />
                    </div>
                    <div class="adminData">
                        <input asp-for="ConfirmPassword" type="password" />
                        <span asp-validation-for="ConfirmPassword"></span>
                    </div>
                </div>
            </div>
        </form>
    }

    if ((ViewBag.ShowFormOnly as bool?) ?? false)
    {
        await RenderForm();
        return;
    }
}

<modal id="change-password-dialog" sm-render-at-page-end="true" sm-size="Medium">
    <modal-header sm-title="@T("Account.ChangePassword")"></modal-header>
    <modal-body>
        @{ await RenderForm(); }
    </modal-body>
    <modal-footer>
        <button class="btn btn-secondary btn-flat" data-dismiss="modal" aria-hidden="true">
            <span>@T("Common.Cancel")</span>
        </button>
        <button id="btnChangePassword" type="button" class="btn btn-secondary btn-to-danger">
            <span>@T("Account.ChangePassword")</span>
        </button>
    </modal-footer>
</modal>

<script sm-target-zone="scripts" data-origin="change-password">
    $(function () {
        $('#btnChangePassword').on('click', function (e) {
            e.preventDefault();

            var dialog = $('#change-password-dialog');
            $.ajax({
				cache: false,
				type: 'POST',
				url: '@(Url.Action("ChangePassword", "Customer", new { id = Model.Id }))',
                data: dialog.find('form:first').serialize(),
                success: function (response) {
                    if (_.isEmpty(response)) {
                        dialog.modal('hide');
                        displayNotification(@T("Admin.Customers.Customers.PasswordChanged").JsValue, 'success');
                    }
                    else {
                        dialog.find('.modal-body').first().html(response);
                    }
                },
                error: function (objXml) {
                    if (objXml != null && objXml.responseText != null && objXml.responseText !== '') {
                        displayNotification(objXml.responseText, 'error');
                    }
                }
            });

            return false;
        });
    });
</script>